package com.example.campusforumapi.controller;

import com.example.campusforumapi.entity.ActivityPartin;
import com.example.campusforumapi.entity.UserRole;
import com.example.campusforumapi.service.ActivityPartService;
import com.example.campusforumapi.util.PageResult;
import com.example.campusforumapi.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/actpartins")
public class ActivityPartController extends BaseController{
    @Autowired
    private ActivityPartService activityPartService;
    /*
     * 按条件  分页查询
     * */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
            , Integer userId ,Integer activityId,String status){
log.info(status);
        //        查询并封装分页结果集
        PageResult actpartPage = activityPartService.getActPartRolePage(page,pageSize,userId,activityId,status);

        return Result.success(actpartPage);
    }
    //    删除一条记录
    @DeleteMapping("/delete/{userIds}/{activityIds}")
    public Result delete(@PathVariable List<Integer> userIds, @PathVariable List<Integer> activityIds){
        activityPartService.delActPart(userIds,activityIds);
        return Result.success();
    }
    //    新增一条记录
    @PostMapping ("/insert")
    public Result insert(@RequestBody ActivityPartin activityPartin){
        if(StringUtils.isEmpty(activityPartin.getUserId()) || StringUtils.isEmpty(activityPartin.getActivityId())){
            return Result.error("用户ID及活动ID不能为空");
        }else if(activityPartService.findsameUserId(activityPartin.getUserId())<1){
            return Result.error("该用户ID不存在，请先注册!");
        }else if(activityPartService.findsameActivityId(activityPartin.getActivityId())<1){
            return Result.error("该活动ID不存在！");
        }
        else if(activityPartService.insertActPart(activityPartin) > 0){
            return Result.success("插入成功");
        }else{
            return Result.error("该用户名与角色关系已存在");
        }

    }
}
